#include "Point.h"
#include "function.h"
#include <GL/glut.h>
#include <cmath>

void peanoD(int n,const Point& a,const Point& b,const Point& c,const Point& d){
	if(n>0){
		double length=(a.x-b.x)/4;

		//calculate the 16 points which the 
		//recursive functions will use
		Point a1(a.x+length,a.y+length,0);
		Point a2(a.x-length,a.y+length,0);
		Point a3(a.x-length,a.y-length,0);
		Point a4(a.x+length,a.y-length,0);

		Point b1(b.x+length,b.y+length,0);
		Point b2(b.x-length,b.y+length,0);
		Point b3(b.x-length,b.y-length,0);
		Point b4(b.x+length,b.y-length,0);

		Point c1(c.x+length,c.y+length,0);
		Point c2(c.x-length,c.y+length,0);
		Point c3(c.x-length,c.y-length,0);
		Point c4(c.x+length,c.y-length,0);

		Point d1(d.x+length,d.y+length,0);
		Point d2(d.x-length,d.y+length,0);
		Point d3(d.x-length,d.y-length,0);
		Point d4(d.x+length,d.y-length,0);

		peanoC(n-1,a1,a2,a3,a4);
		peanoD(n-1,b1,b2,b3,b4);
		peanoD(n-1,c1,c2,c3,c4);
		peanoA(n-1,d1,d2,d3,d4);
		
		//calculate the distance which the given point move
		const double offset=(2-pow(0.5,n-2))*length;

		//draw the lines to connect the components
		glBegin(GL_LINE_STRIP);
			glVertex3f(a.x-offset,a.y+offset,a.z);
			glVertex3f(b.x+offset,b.y+offset,b.z);
		glEnd();

		glBegin(GL_LINE_STRIP);
			glVertex3f(b.x+offset,b.y-offset,b.z);
			glVertex3f(c.x+offset,c.y+offset,c.z);
		glEnd();

		glBegin(GL_LINE_STRIP);
			glVertex3f(d.x-offset,d.y-offset,d.z);
			glVertex3f(c.x+offset,c.y-offset,c.z);
		glEnd();
	}
}
